package org.javaboy.vhr.service;

import org.javaboy.vhr.mapper.EmployeeecMapper;
import org.javaboy.vhr.model.Employeeec;
import org.javaboy.vhr.model.RespPageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class EmployeeecService {
    @Autowired
    EmployeeecMapper employeeecMapper;

    // 奖惩状态常量
    public static final String STATUS_PENDING = "待审核";
    public static final String STATUS_APPROVED = "已通过";
    public static final String STATUS_REJECTED = "已拒绝";

    /**
     * 获取所有员工奖惩记录（分页）
     * @param page 页码
     * @param size 每页大小
     * @param keyword 关键词搜索
     * @return 奖惩记录列表
     */
    public RespPageBean getAllEmpec(Integer page, Integer size, String keyword) {
        if (page != null && size != null) {
            page = (page - 1) * size;
        }
        List<Employeeec> data = employeeecMapper.getAllEmpec(page, size, keyword);
        Long total = employeeecMapper.getTotal(keyword);
        RespPageBean bean = new RespPageBean();
        bean.setTotal(total);
        bean.setData(data);
        return bean;
    }

    /**
     * 获取指定员工的奖惩记录
     * @param eid 员工ID
     * @return 奖惩记录列表
     */
    public List<Employeeec> getEmpecByEid(Integer eid) {
        return employeeecMapper.getEmpecByEid(eid);
    }

    /**
     * 添加奖惩记录
     * @param employeeec 奖惩记录对象
     * @return 影响行数
     */
    public int addEmpec(Employeeec employeeec) {
        if (employeeec.getEcdate() == null) {
            employeeec.setEcdate(new Date());
        }
        // 设置默认状态为待审核
        if (employeeec.getStatus() == null || employeeec.getStatus().isEmpty()) {
            employeeec.setStatus(STATUS_PENDING);
        }
        return employeeecMapper.insertSelective(employeeec);
    }

    /**
     * 更新奖惩记录
     * @param employeeec 奖惩记录对象
     * @return 影响行数
     */
    public int updateEmpec(Employeeec employeeec) {
        // 更新时将状态重置为待审核
        if (employeeec.getStatus() == null) {
            // 如果status为空，将状态重置为待审核
            employeeec.setStatus(STATUS_PENDING);
        }
        return employeeecMapper.updateByPrimaryKeySelective(employeeec);
    }

    /**
     * 删除奖惩记录
     * @param id 记录ID
     * @return 影响行数
     */
    public int deleteEmpec(Integer id) {
        return employeeecMapper.deleteByPrimaryKey(id);
    }

    /**
     * 批量删除奖惩记录
     * @param ids 记录ID数组
     * @return 影响行数
     */
    public int deleteEmpecByIds(Integer[] ids) {
        return employeeecMapper.deleteEmpecByIds(ids);
    }

    /**
     * 获取单个奖惩记录
     * @param id 记录ID
     * @return 奖惩记录对象
     */
    public Employeeec getEmpecById(Integer id) {
        return employeeecMapper.selectByPrimaryKey(id);
    }
}
